﻿//Original Form Size 1033,737
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATCSim.Objects;

namespace ATCSim
{
    public partial class MainDisplay : Form
    {
        private Timer _t;
        AircraftManager _AirManager;
        Aircraft _SelectedAircraft;
        PointF _HeadingWheelLine = new PointF(83, 153);

        public MainDisplay()
        {
            InitializeComponent();
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            _AirManager = new AircraftManager(listTakeOff);

            _t = new Timer();
            _t.Interval = 3000; //3000 is default for correct ratio of movement
            _t.Tick += new System.EventHandler(TimerOnTick);
            _t.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //gets the screen resolution
            //Screen scn = Screen.PrimaryScreen;
            this.Text = "ATC Simulator";
        }

        private void TimerOnTick(object sender, System.EventArgs e)
        {
            this.Refresh(); //Refresh the form
            //this.Text = DateTime.Now.TimeOfDay.ToString();
            UpdateStatusBar();
            _AirManager.UpdateAllAircraft(_t);
            textAircraftID.Text = textAircraftID.Text;
        }

        private void UpdateStatusBar()
        {
            Aircraft tmpAircraft = _AirManager.FindAircraft(textAircraftID.Text.ToUpper());
            if (tmpAircraft != null)
            {
                toolsLabelAircraftID.Text = "Aircraft ID: " + tmpAircraft.ID.ToString() + " |";
                toolLabelAltitude.Text = "Altitude: " + tmpAircraft.Altitude.ToString() + " |";
                toolLabelHeading.Text = "Heading: " + tmpAircraft.Heading.ToString() + " |";
                toolLabelSpeed.Text = "Speed: " + tmpAircraft.Speed.ToString() + " |";
                toolLabelTurningTo.Text = "Turning To: " + tmpAircraft.ToHeading.ToString() + " |";
                toolLabelGoingToAlt.Text = "Going To Altitude: " + tmpAircraft.ToAltitude.ToString() + " |";
                toolLabelToSpeed.Text = "Going To Speed: " + tmpAircraft.ToSpeed.ToString();
            }
            else
            {
                //trackBarAltitude.Value = 1000;
                toolsLabelAircraftID.Text = "Aircraft ID: |";
                toolLabelAltitude.Text = "Altitude: |";
                toolLabelHeading.Text = "Heading: |";
                toolLabelSpeed.Text = "Speed: |";
                toolLabelTurningTo.Text = "Turning To: |";
                toolLabelGoingToAlt.Text = "Going To Altitude: |";
                toolLabelToSpeed.Text = "Going To Speed: ";
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            #region DrawAirportAndScreenSetup
            //Draw Divider
            e.Graphics.DrawLine(new Pen(Color.Black), new Point(200, 0), new Point(200, 768));

            //Circles for radar
            e.Graphics.DrawEllipse(new Pen(Color.Green,3), new Rectangle(285, 30, 650, 650));
            e.Graphics.DrawEllipse(new Pen(Color.Green), new Rectangle(362, 103, 500, 500));
            e.Graphics.DrawEllipse(new Pen(Color.Green), new Rectangle(435, 175, 350, 350));
            e.Graphics.DrawEllipse(new Pen(Color.Green), new Rectangle(510, 245, 200, 200));
            
            //Draw airport
            Brush b = Brushes.Black;
            e.Graphics.DrawRectangle(new Pen(Color.MediumBlue), 600, 305, 20, 70);
            e.Graphics.DrawLine(new Pen(Color.Red), new Point(610, 305), new Point(610, 270));
            e.Graphics.DrawLine(new Pen(Color.Red), new Point(610, 375), new Point(610, 410));
            e.Graphics.DrawString("18", new Font("Arial", 7), b, 602, 310);
            e.Graphics.DrawString("36", new Font("Arial", 7), b, 602, 360);

            Brush b1 = Brushes.Black;
            e.Graphics.DrawString("50",new Font("Arial", 16), b1, 370, 105);
            e.Graphics.DrawString("40", new Font("Arial", 16), b1, 430, 160);
            e.Graphics.DrawString("30", new Font("Arial", 16), b1, 485, 210);
            e.Graphics.DrawString("20", new Font("Arial", 16), b1, 530, 260);

            //Draw Enter/Exit
            e.Graphics.DrawLine(new Pen(Color.Green,3), new Point(255, 325), new Point(320, 325));
            e.Graphics.DrawLine(new Pen(Color.Green,3), new Point(255, 385), new Point(320, 385));

            e.Graphics.DrawLine(new Pen(Color.Green, 3), new Point(900, 325), new Point(965, 325));
            e.Graphics.DrawLine(new Pen(Color.Green, 3), new Point(900, 385), new Point(965, 385));

            //Draw Heading Wheel
            e.Graphics.DrawEllipse(new Pen(Brushes.Black), 25, 95, 116, 116);
            //e.Graphics.FillEllipse(Brushes.Black, 81, 151, 3, 3);
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new PointF(83, 153), _HeadingWheelLine);

            //Holding Circles
            e.Graphics.DrawEllipse(new Pen(Brushes.Maroon, 2), 210, 10, 160, 160);
            e.Graphics.DrawEllipse(new Pen(Brushes.Maroon, 2), 210, 550, 160, 160);
            e.Graphics.DrawEllipse(new Pen(Brushes.Maroon, 2), 860, 550, 160, 160);
            e.Graphics.DrawEllipse(new Pen(Brushes.Maroon, 2), 860, 10, 160, 160);
            #endregion
            //Draw Airplanes
            _AirManager.DrawAllAircraft(e);
        }

        private void speed_change(object sender, EventArgs e)
        {
            _t.Interval = Math.Abs(trackBarSpeed.Value);
        }

        private void textAircraftID_TextChanged(object sender, EventArgs e)
        {
            Aircraft tmpAircraft = _AirManager.FindAircraft(textAircraftID.Text.ToUpper());
            trackAircraftSpeed.Minimum = 65;
            if (tmpAircraft != null)
            {
                trackBarAltitude.Value = tmpAircraft.Altitude;
                trackAircraftSpeed.Value = tmpAircraft.Speed;
                labelAircraftSpeed.Text = "Aicraft Speed: " + tmpAircraft.Speed.ToString() + " kts";
                labelAltitude.Text = "Altitude: " + tmpAircraft.Altitude.ToString() + " ft";
                toolsLabelAircraftID.Text = "Aircraft ID: " + tmpAircraft.ID.ToString() +" |";
                toolLabelAltitude.Text = "Altitude: " + tmpAircraft.Altitude.ToString() + " |";
                toolLabelHeading.Text = "Heading: " + tmpAircraft.Heading.ToString() + " |";
                toolLabelSpeed.Text = "Speed: " + tmpAircraft.Speed.ToString() + " |";
                toolLabelTurningTo.Text = "Turning To: " + tmpAircraft.ToHeading.ToString() + " |";
                toolLabelGoingToAlt.Text = "Going To Altitude: " + tmpAircraft.ToAltitude.ToString() + " |";
                toolLabelToSpeed.Text = "Going To Speed: " + tmpAircraft.ToSpeed.ToString();
            }
            else
            {
                trackBarAltitude.Value = 1000;
                trackAircraftSpeed.Value = 65;
                labelAltitude.Text = "Altitude: ";
                labelAircraftSpeed.Text = "Aicraft Speed: ";
                toolsLabelAircraftID.Text = "Aircraft ID: |";
                toolLabelAltitude.Text = "Altitude: |";
                toolLabelHeading.Text = "Heading: |";
                toolLabelSpeed.Text = "Speed: |";
                toolLabelTurningTo.Text = "Turning To: |";
                toolLabelGoingToAlt.Text = "Going To Altitude: ";
                toolLabelToSpeed.Text = "Going To Speed: ";
            }
        }

        private void trackBarAltitude_Scroll(object sender, EventArgs e)
        {
            labelAltitude.Text = "Altitude: " + trackBarAltitude.Value.ToString();
            _AirManager.GoToAltitude(textAircraftID.Text.ToUpper(), trackBarAltitude.Value);
        }

        private void trackAircraftSpeed_Scroll(object sender, EventArgs e)
        {
            if(_AirManager.FindAircraft(textAircraftID.Text) != null)
                trackAircraftSpeed.Minimum = _AirManager.FindAircraft(textAircraftID.Text).MinSpeed;
            labelAircraftSpeed.Text = "Aicraft Speed: " + trackAircraftSpeed.Value.ToString() + " kts";
            _AirManager.GoToSpeed(textAircraftID.Text, trackAircraftSpeed.Value);
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            MouseEventArgs mouse = (MouseEventArgs)e;
            #region CheckClickAircraft
            if (!(Math.Pow(mouse.X - 83, 2) + Math.Pow(mouse.Y - 153, 2) < Math.Pow(58, 2)))
            {
                _HeadingWheelLine = new PointF(83, 153);
                this.Refresh();
                if (mouse.Button.CompareTo(MouseButtons.Left) == 0)
                {
                    if (_SelectedAircraft != null)
                        _SelectedAircraft.AircraftColor = Brushes.Black;
                    _SelectedAircraft = _AirManager.IsXYWithinAircraft((float)mouse.X, (float)mouse.Y);
                    if (_SelectedAircraft != null)
                    {
                        textAircraftID.Text = _SelectedAircraft.ID;
                        _SelectedAircraft.AircraftColor = Brushes.Blue;
                        this.Refresh();
                    }
                    else
                    {
                        textAircraftID.Text = "";
                    }
                }
                if (mouse.Button.CompareTo(MouseButtons.Right) == 0)
                {
                    if(_SelectedAircraft != null)
                        contextMenuStrip.Show((Control)sender, e.X,e.Y);
                }
            }
            #endregion
            #region HeadingWheel
            if (Math.Pow(mouse.X - 83, 2) + Math.Pow(mouse.Y - 153, 2) < Math.Pow(58, 2))
            {
                int x, y;
                x = mouse.X - 83;
                y = -(mouse.Y - 153);
                double degree = 0;
                if (x > 0 && y >= 0)
                {
                    degree = Math.Atan2(y, x);
                    degree -= Math.PI/2;
                }
                if (x < 0 && y > 0)
                {
                    degree = Math.Atan2(-y, -x);
                    degree -= 3*Math.PI/2;
                }
                if (x < 0 && y < 0)
                {
                    degree = Math.Atan2(y, x);
                    degree -= Math.PI/2;
                }
                if (x > 0 && y < 0)
                {
                    degree = Math.Atan2(y, x);
                    degree -= Math.PI/2;
                }
                if (y == 0 && x < 0)
                    degree = 3*Math.PI/2;
                if (x == 0 && y > 0)
                    degree = 0;
                if (x == 0 && y < 0)
                    degree = Math.PI;

                _HeadingWheelLine.X = mouse.X;
                _HeadingWheelLine.Y = mouse.Y;

                degree = Math.Round(degree * (180 / Math.PI), 0);
                degree = Math.Abs(degree);
                labelHeading.Text = "Heading: " + degree;
                this.Refresh();
                if (mouse.Button.CompareTo(MouseButtons.Left) == 0)
                {
                    if (_SelectedAircraft != null)
                        _AirManager.TurnToHeading(_SelectedAircraft.ID, (int)degree, true);
                }
                if (mouse.Button.CompareTo(MouseButtons.Right) == 0)
                {
                    if (_SelectedAircraft != null)
                        _AirManager.TurnToHeading(_SelectedAircraft.ID, (int)degree, false);
                }
            }
            #endregion
        }

        private void button_SpawnAircraft_Click(object sender, EventArgs e)
        {
            bool DidWeSpawn = _AirManager.SpawnAircraft();
            if (DidWeSpawn == false)
                MessageBox.Show("Move Aircraft Into Airspace First","Cant Spawn");
            this.Refresh();
        }

        private void button_EnterAirspace_Click(object sender, EventArgs e)
        {
            if(_SelectedAircraft!= null)
            _AirManager.ComeIntoAirspace(_SelectedAircraft);
        }
        private void AircraftTakeOff(object sender, EventArgs e)
        {
            if (listTakeOff.SelectedItem != null)
            {
                _AirManager.Airport.TakeOff((String)listTakeOff.SelectedItem);
                listTakeOff.Items.RemoveAt(listTakeOff.SelectedIndex);
            }
        }

        private void Left0(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 0, true);
        }

        private void Left90(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 90, true);
        }

        private void Left180(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 180, true);
        }

        private void Left270(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 270, true);
        }

        private void Right0(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 0, false);
        }

        private void Right90(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 90, false);
        }

        private void Right180(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 180, false);
        }

        private void Right270(object sender, EventArgs e)
        {
            _AirManager.TurnToHeading(_SelectedAircraft.ID, 270, false);
        }

        private void GoToAlt1000(object sender, EventArgs e)
        {
            labelAltitude.Text = "Altitude: " + 1000;
            trackBarAltitude.Value = 1000;
            _AirManager.GoToAltitude(textAircraftID.Text.ToUpper(), 1000);
        }

        private void GoToAlt5000(object sender, EventArgs e)
        {
            labelAltitude.Text = "Altitude: " + 5000;
            trackBarAltitude.Value = 5000;
            _AirManager.GoToAltitude(textAircraftID.Text.ToUpper(), 5000);
        }

        private void GoToAlt10000(object sender, EventArgs e)
        {
            labelAltitude.Text = "Altitude: " + 10000;
            trackBarAltitude.Value = 10000;
            _AirManager.GoToAltitude(textAircraftID.Text.ToUpper(), 10000);
        }

        private void Speed65(object sender, EventArgs e)
        {
            trackAircraftSpeed.Value = 65;
            labelAircraftSpeed.Text = "Aicraft Speed: " + "65" + " kts";
            _AirManager.GoToSpeed(textAircraftID.Text, 65);
        }

        private void Speed150(object sender, EventArgs e)
        {
            trackAircraftSpeed.Value = 150;
            labelAircraftSpeed.Text = "Aicraft Speed: " + "150" + " kts";
            _AirManager.GoToSpeed(textAircraftID.Text, 150);
        }

        private void Speed250(object sender, EventArgs e)
        {
            trackAircraftSpeed.Value = 250;
            labelAircraftSpeed.Text = "Aicraft Speed: " + "250" + " kts";
            _AirManager.GoToSpeed(textAircraftID.Text, 250);
        }

        private void CheckMinAicraftSpeed(object sender, EventArgs e)
        {
            if (_AirManager.FindAircraft(textAircraftID.Text).MinSpeed == 65)
            {
                trackAircraftSpeed.Minimum = 65;
                goToSpeedToolStripMenuItem.DropDownItems.Find("toolStripMenuItem13", false)[0].Enabled = true;
            }
            else
            {
                trackAircraftSpeed.Minimum = 150;
                goToSpeedToolStripMenuItem.DropDownItems.Find("toolStripMenuItem13", false)[0].Enabled = false;
            }
        }
    }
}
